package com.bw.shopping.controller;

import com.bw.common.core.domain.R;
import com.bw.common.core.web.controller.BaseController;
import com.bw.shopping.domain.NewShoppingCart;
import com.bw.shopping.domain.PmsBrand;
import com.bw.shopping.service.NewShoppingCartService;
import com.bw.shopping.vo.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：尚志豪
 * @Package：com.bw.goods.controller
 * @Project：ruoyiszh
 * @name：DetailsInformationController
 * @Date：2024/12/23 20:33
 */
@RestController
@RequestMapping("/newShoppingCart")
public class NewShoppingCartController extends BaseController {

    @Autowired
    private NewShoppingCartService newShoppingCartService;


    /**
     * 添加购物车功能【插入】
     */
    @PostMapping("/addShoppingCartFunction")
    public R addShoppingCartFunction(@RequestBody NewShoppingCart newShoppingCart)
    {
        System.out.println("参数: "+newShoppingCart);
        // 根据id品牌查询名称
        PmsBrand pmsBrand = newShoppingCartService.searchForNameBasedOnId(newShoppingCart.getShopsId());
        newShoppingCart.setShopsName(pmsBrand.getName());
        newShoppingCartService.addShoppingCartFunction(newShoppingCart);
        return R.ok("添加购物车功能成功");
    }
    /**
     * 基于redis实现添加购物车
     */
    @PostMapping("/addToCart")
    public void addToCart(@RequestBody NewShoppingCart cartItem){
        // 获取skuId
        Long skuId = cartItem.getSkuId();

        // 根据id品牌查询名称
        PmsBrand pmsBrand = newShoppingCartService.searchForNameBasedOnId(cartItem.getShopsId());
        cartItem.setShopsName(pmsBrand.getName());
        newShoppingCartService.addToCart(cartItem);
    }

    /**
     * redis查询出的结构要进行结构转换
     */
    @GetMapping("/getCart")
    public Cart getCart(@RequestParam String userId) {
        return newShoppingCartService.getCartFromRedis(userId);
    }
}
